<?php 
class CartPrint extends Module
{
	static $cart = array(), $cart_items = array(), $items = array();
	
	function CartPrint($row)
	{
		Module::Module($row);
		
		CGlobal::$website_title = "in đơn hàng";
		
		$id	=	URL::get('id',0);
		
		if($id)
		{
			CartPrint::$cart = DB::select("cart","id=$id");
			
			if(CartPrint::$cart && (User::have_permit(ADMIN_ITEM) || CartPrint::$cart['user_id'] == User::id() || CartPrint::$cart['session'] == CGlobal::$cart_sess_id))
			{
				$re = DB::query("SELECT * FROM cart_item WHERE cart_id=".CartPrint::$cart['id']." ORDER BY id DESC");
				
				$cart_item_del 	= '';
				$item_ids 		= '';
				
				if($re)
				{
					while($cart_item = mysql_fetch_assoc($re))
					{
						$item_ids .= ($item_ids != '' ? ',' : '') . $cart_item['item_id'];
						
						CartPrint::$cart_items[$cart_item['id']]	= $cart_item;
					}
				}
			
				if($item_ids != '')
				{
					$re = DB::query("SELECT * FROM item WHERE id IN(".$item_ids.") AND status=1 AND price>0");
				
					if($re)
					{
						while($item = mysql_fetch_assoc($re))
						{
							$item['price'] = number_format(AZLib::convertCurrency($item['price'], $item['currency_id']),null,null,'.');
							
							CartPrint::$items[$item['id']]	= $item;		
						}
					}
				}

				$cart_item_del = '';
				
				foreach (CartPrint::$cart_items as $cart_item)
				{
					if(isset(CartPrint::$items[$cart_item['item_id']]))
					{
						if($cart_item['price'] != CartPrint::$items[$cart_item['item_id']]['price'] || $cart_item['sku'] != CartPrint::$items[$cart_item['item_id']]['sku'])
						{
							$cart_item['price'] = CartPrint::$items[$cart_item['item_id']]['price'];
							$cart_item['sku'] 	= CartPrint::$items[$cart_item['item_id']]['sku'];
							
							DB::update('cart_item',array('price'=>$cart_item['price'],'sku'=>$cart_item['sku']),"id={$cart_item['id']}");
						}
						
						CartPrint::$cart_items[$cart_item['id']]	= $cart_item;
					}
					else
					{
						$cart_item_del .= ($cart_item_del != '' ? ',' : '') . $cart_item['id'];
					}
				}
				
				if($cart_item_del != '')
				{
					DB::delete('cart_item',"id IN($cart_item_del)");
				}
			}
			else
			{
				AZLib::closePopup();
				exit;
			}
		}
		
		if(!CartPrint::$cart)
		{
			AZLib::closePopup();
			exit;
		}
		
		require_once 'forms/CartPrint.php';			
		$this->add_form(new CartPrintForm());
	}
}
?>